﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataAccess;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.Native;
using BLL;

namespace QuanLyKhachSan.FormTimKiem
{
    public partial class FormTimPhong : XtraForm
    {
        public FormTimPhong()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            var findType = TimTheo();
            String content = txtNoiDungTim.Text;
            IEnumerable<Exception> errors = KiemTraGiaTriHopLe(findType, content);
            var exceptions = errors as Exception[] ?? errors.ToArray();
            if (!exceptions.Any())
            {
                Func<Phong, bool> newFilter;
                switch (findType)
                {
                    case TimPhongTheo.TimTheoTrangThaiPhong:
                        newFilter = p => p.TrangThaiPhong.Contains(content);
                        break;
                    case TimPhongTheo.TimTheoSucChua:
                        int sc = int.Parse(content);
                        newFilter = p => p.SucChua.Equals(sc);
                        break;
                    default:
                        newFilter = p => p.LoaiPhong.TenLoaiPhong.Contains(content);
                        break;
                }
                var list = new PhongManager().LayDanhSach().Result.Where(newFilter);
                var phongs = list as Phong[] ?? list.ToArray();
                if (!phongs.Any())
                {
                    XtraMessageBox.Show("Không tìm thấy");
                }
                else
                {
                    Result = phongs;
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                lblInfo.Text = exceptions.ToList()[0].ToString();
            }
        }

        private void FormTimPhong_Load(object sender, EventArgs e)
        {
            cbbLoaiTim.SelectedIndex = 0;
        }

        private TimPhongTheo TimTheo()
        {
            String findType = cbbLoaiTim.Text;
            switch (findType)
            {
                case "Tìm theo trạng thái phòng":
                    return TimPhongTheo.TimTheoTrangThaiPhong;
                case "Tìm theo sức chứa":
                    return TimPhongTheo.TimTheoSucChua;
                default:
                    return TimPhongTheo.TimTheoLoaiPhong;
            }
        }

        private IEnumerable<Exception> KiemTraGiaTriHopLe(TimPhongTheo loaiTim, String value)
        {
            var exceptions = new List<Exception>();
            switch (loaiTim)
            {
                case TimPhongTheo.TimTheoTrangThaiPhong:
                    if (value.IsEmpty())
                        exceptions.Add(new Exception("Vui lòng nhập trạng thái cần tìm"));
                    break;
                case TimPhongTheo.TimTheoSucChua:
                    try
                    {
                        int.Parse(value);
                    }
                    catch (Exception)
                    {
                        exceptions.Add(new Exception("Sức chứa phải là số nguyên"));
                    } 
                    break;
                default:
                    if (value.IsEmpty())
                        exceptions.Add(new Exception("Vui lòng nhập loại phòng cần tìm"));
                    break;
            }
            return exceptions;
        }

        public Func<Phong, bool> Filter { get; set; }

        public IEnumerable<Phong> Result { get; set; }

        private void txtNoiDungTim_TextChanged(object sender, EventArgs e)
        {
            lblInfo.Text = "";
        }

    }
}
